
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWidget>
#include <QLabel>
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
#include <QDebug>

#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>

#include <QLinkedList>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    pw = new QWidget;
    pw->setGeometry(400,300,200,100);
    pthread = new WorkThread();

    scroll = new QScrollArea();
    ui->gridLayout->addWidget(scroll,3,0);
    pter = new Painter(scroll);
    pter->setGeometry(0,0,width()+1000,height()+500);
    scroll->setWidget(pter);
}

MainWindow::~MainWindow()
{
    delete pter;
    delete scroll;
    delete pw;
    delete ui;
    delete pthread;
}

void MainWindow::on_pushButton_clicked()
{
    QMessageBox::information(this,tr("message"),"测试显示中文");
    pw->setWindowTitle(tr("Enter your age:"));
    QLabel *l = new QLabel(pw);
    QString str = tr("Hello Qt!");
    l->setText(str);

    QSpinBox *spinBox = new QSpinBox;
    QSlider *slider = new QSlider(Qt::Horizontal);
    spinBox->setRange(0, 130);
    slider->setRange(0, 130);

    QObject::connect(spinBox, SIGNAL(valueChanged(int)),
                     slider, SLOT(setValue(int)));
    QObject::connect(slider, SIGNAL(valueChanged(int)),
                     spinBox, SLOT(setValue(int)));
    spinBox->setValue(24);

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(spinBox);
    layout->addWidget(slider);
    pw->setLayout(layout);

    pw->show();
}

void MainWindow::on_btnReadFile_clicked()
{
    QFile file("main.cpp");
    if(file.open(QIODevice::ReadOnly))
    {
        ui->statusBar->showMessage(tr("Open file main.cpp OK!"),1000);
        char buffer[512];
        qint64 len = file.read(buffer,sizeof(buffer));
        if(len!=-1)
        {
            ui->statusBar->showMessage(tr("Read file main.cpp OK!"),1000);
            QMessageBox::information(this,tr("message"),QString(buffer));
        }

#ifdef use_Stream
        QTextStream in(&file);
        in>>buffer;
        QDataStream s;//bin data
#endif
    }
}

void WorkThread::run()
{
    qDebug()<<tr("In the independent thread...");
    //run
    //use Containers
    //存储在Qt的容器中的数据必须是可赋值的数据类型，
    //即有默认构造函数，拷贝构造函数以及赋值操作运算符
    //QObject及其子类不能存储在容器中。
    QHash<QString, QList<double> > list;

    QList<int> il;
    il.append(4);
    il.prepend(5);
    il<<7;
    QListIterator<int> iter(il);
    for(;iter.hasNext();)
        qDebug()<<iter.next();
    //继承自QList有:QItemSelection,QQueue,QSignalSpy,QStringList,QTestEventList
    QLinkedList<int> ill;
    QVector<int> v;
    v<<4<<7<<9;
    QVector<int>::Iterator i;
    for(i=v.begin();i!=v.end();++i)
        qDebug()<< (*i);

    QString str = "How ";
    str += "are ";
    str.append("you?");
    QString sstr;
    sstr.sprintf("%s am %d.","I",24);
    QString str2 = QString("%1 was born in %2").arg("John").arg(1989);
    QString stri = "128";
    bool ok;
    int hex = stri.toInt(&ok,16);
    int dec = stri.toInt(&ok);
    QString stri2;
    stri2.sprintf("%s hex is %d. the dec is %d",qPrintable(stri),hex,dec);
    //use str.toLocal8Bit().constData()
    //or qPrintable(str) to const char*
    qDebug(str.toLocal8Bit().constData());
    qDebug(qPrintable(sstr));
    qDebug()<<str2;
    qDebug()<<stri2;

    //QVariant
    //QRegExp
}

void MainWindow::on_btnThread_clicked()
{
    pthread->start();
}


